﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMS.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using LibraryONTrack.dto;
using LibraryONTrack.dao;
using LibraryONTrack.Utility;

namespace LibraryONTrack.UI
{
	public partial class PrintNoticesPage : SMS.Windows.Forms.InteriorWizardPage
	{
        private String stringToPrint = null;
        private String numOfSpaces = "  ";
        Print print;
        OverdueBookDAO overdueBookDAO;
        List<OverdueBook> overdueBookList;

		public PrintNoticesPage()
		{
			InitializeComponent();
		}

		protected override bool OnSetActive()
		{
			if (!base.OnSetActive())
				return false;

			Wizard.SetWizardButtons(WizardButton.Back | WizardButton.Finish);

			overdueBookDAO = new OverdueBookDAO();

			Level1NoticeCountTextBox.Text = overdueBookDAO.GetOverdueNoticeCount(1).ToString();
			Level2NoticeCountTextBox.Text = overdueBookDAO.GetOverdueNoticeCount(2).ToString();
			Level3NoticeCountTextBox.Text = overdueBookDAO.GetOverdueNoticeCount(3).ToString();
			
		return true;
			
		}

		protected override bool OnWizardFinish()
		{
			return true;			
		}

        private void PrintOverdueBooksButton_Click(object sender, EventArgs e)
        {
            try
            {
                overdueBookList = overdueBookDAO.GetAllOverdueBooks();   
                
                for (int i = 0; i < overdueBookList.Count; i++)
                {
                    stringToPrint += "*******************************************\n\n";
                    stringToPrint += "Library ONtrack\n\n";
                    stringToPrint += "Book Title:" + numOfSpaces + overdueBookList[i].Book.Title + "\n";
                    stringToPrint += "Student Name:" + numOfSpaces + overdueBookList[i].Borrower + "\n";
                    stringToPrint += "Teacher Name:" + numOfSpaces + overdueBookList[i].Teacher + "\n";
                    stringToPrint += "Date of Notice:" + numOfSpaces + DateTime.Now + "\n";
                    stringToPrint += "Notice Number:" + numOfSpaces + overdueBookList[i].NoticeCount + "\n\n";
                    stringToPrint += "This book is overdue. Please return it promptly.\n\n";
                    stringToPrint += "*******************************************\n\n\n\n";
                    
                    //two checks are done here:
                    //#1 - gets two notices per page, if it's not an even number
                    //     notice, it won't print.
                    //#2 - if it's the last notice in the list, it will print it
                    //     regardless if it's even or not.
                    if ((i % 2 == 0) || (i == overdueBookList.Count-1))
                    {
                        print = new Print();
											
                        print.PrintNotice(stringToPrint);
                        stringToPrint = null;
                    }
                    //update book's last_notice_sent_unixtime
                    overdueBookList[i].LastNoticeSentDateTime = DateTime.Now;
                    overdueBookDAO.Update(overdueBookList[i]); 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PrintLibrarianReportButton_Click(object sender, EventArgs e)
        {
            //TO-DO
            MessageBox.Show("Not implemented");
        }

        
	}
}
